/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import xowl.utils.config.Configuration;
import xowl.utils.config.Section;

/**
 *
 * @author Laurent WOUTERS
 */
public class Diagram {
    private String id;
    private String name;
    private WorkbenchView view;
    private String root;
    private List<DiagramElement> elements;
    
    public String getID() { return id; }
    public String getName() { return name; }
    public WorkbenchView getWBView() { return view; }
    public String getRoot() { return root; }
    
    public void setRoot(String root) { this.root = root; }
    public void addElement(DiagramElement elem) { elements.add(elem); }
    public Collection<DiagramElement> getElements() { return elements; }
    
    public Diagram(String name, WorkbenchView view) {
        this.id = "Diagram" + Integer.toHexString(this.hashCode());
        this.name = name;
        this.view = view;
        this.elements = new ArrayList<>();
    }
    
    public Diagram(String name, WorkbenchView view, String root) {
        this.id = "Diagram" + Integer.toHexString(this.hashCode());
        this.name = name;
        this.view = view;
        this.root = root;
        this.elements = new ArrayList<>();
    }
    
    public Diagram(String id, Configuration config, Workbench workbench) {
        Section section = config.getSection(id);
        this.id = id;
        this.name = section.getValue("name");
        this.view = workbench.getView(section.getValue("view"));
        this.elements = new ArrayList<>();
        this.root = section.getValue("root");
        for (String elemID : section.getValues("element")) {
            if (elemID.startsWith("Node"))
                this.elements.add(new DiagramNode(elemID, section));
            if (elemID.startsWith("Link"))
                this.elements.add(new DiagramLink(elemID, section));
            if (elemID.startsWith("Relation"))
                this.elements.add(new DiagramRelation(elemID, section));
        }
    }
    
    public void clear() {
        this.root = null;
        this.elements.clear();
    }
    
    public void save(Configuration config) {
        Section section = config.getSection(id);
        section.addValue("name", name);
        section.addValue("view", view.getID());
        section.addValue("root", root);
        for (DiagramElement elem : elements) {
            section.addValue("element", elem.getID());
            elem.save(section);
        }
    }
}
